<template>
  <el-dialog
    :title="!dataForm.omsGroupId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="团长id(用户id)" prop="umsUserId">
      <el-input v-model="dataForm.umsUserId" placeholder="团长id(用户id)"></el-input>
    </el-form-item>
    <el-form-item label="团购商品id" prop="smsTgouId">
      <el-input v-model="dataForm.smsTgouId" placeholder="团购商品id"></el-input>
    </el-form-item>
    <el-form-item label="成团人数" prop="smsTgouGroupNum">
      <el-input v-model="dataForm.smsTgouGroupNum" placeholder="成团人数"></el-input>
    </el-form-item>
    <el-form-item label="拼团人数" prop="omsGroupPersonNum">
      <el-input v-model="dataForm.omsGroupPersonNum" placeholder="拼团人数"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="omsGroupCreateDate">
      <el-input v-model="dataForm.omsGroupCreateDate" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="结束时间" prop="omsGroupEndDate">
      <el-input v-model="dataForm.omsGroupEndDate" placeholder="结束时间"></el-input>
    </el-form-item>
    <el-form-item label="状态 0:未进行 1.代付款 2.拼团中 3.拼团成功 4.拼团失败 " prop="omsGroupStatus">
      <el-input v-model="dataForm.omsGroupStatus" placeholder="状态 0:未进行 1.代付款 2.拼团中 3.拼团成功 4.拼团失败 "></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          omsGroupId: 0,
          umsUserId: '',
          smsTgouId: '',
          smsTgouGroupNum: '',
          omsGroupPersonNum: '',
          omsGroupCreateDate: '',
          omsGroupEndDate: '',
          omsGroupStatus: ''
        },
        dataRule: {
          umsUserId: [
            { required: true, message: '团长id(用户id)不能为空', trigger: 'blur' }
          ],
          smsTgouId: [
            { required: true, message: '团购商品id不能为空', trigger: 'blur' }
          ],
          smsTgouGroupNum: [
            { required: true, message: '成团人数不能为空', trigger: 'blur' }
          ],
          omsGroupPersonNum: [
            { required: true, message: '拼团人数不能为空', trigger: 'blur' }
          ],
          omsGroupCreateDate: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          omsGroupEndDate: [
            { required: true, message: '结束时间不能为空', trigger: 'blur' }
          ],
          omsGroupStatus: [
            { required: true, message: '状态 0:未进行 1.代付款 2.拼团中 3.拼团成功 4.拼团失败 不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.omsGroupId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.omsGroupId) {
            this.$http({
              url: this.$http.adornUrl(`/order/group/info/${this.dataForm.omsGroupId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.umsUserId = data.group.umsUserId
                this.dataForm.smsTgouId = data.group.smsTgouId
                this.dataForm.smsTgouGroupNum = data.group.smsTgouGroupNum
                this.dataForm.omsGroupPersonNum = data.group.omsGroupPersonNum
                this.dataForm.omsGroupCreateDate = data.group.omsGroupCreateDate
                this.dataForm.omsGroupEndDate = data.group.omsGroupEndDate
                this.dataForm.omsGroupStatus = data.group.omsGroupStatus
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/order/group/${!this.dataForm.omsGroupId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'omsGroupId': this.dataForm.omsGroupId || undefined,
                'umsUserId': this.dataForm.umsUserId,
                'smsTgouId': this.dataForm.smsTgouId,
                'smsTgouGroupNum': this.dataForm.smsTgouGroupNum,
                'omsGroupPersonNum': this.dataForm.omsGroupPersonNum,
                'omsGroupCreateDate': this.dataForm.omsGroupCreateDate,
                'omsGroupEndDate': this.dataForm.omsGroupEndDate,
                'omsGroupStatus': this.dataForm.omsGroupStatus
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
